#include "kvs.h"
// 测试

int kvs_rbtree_create(rbtree_t **rbt)
{
    if (!rbt)
        return -1;
    if (*rbt)
        return -1;

    *rbt = rb_init();
#if (FULL_PERSIST && INIT_BY_FULL)
    LOG("正在同步红黑树全量持久化: %s\n", RB_FULL_FILE_NAME);
    int ret = rb_full_restore(RB_FULL_FILE_NAME, *rbt);
    if (ret == -1)
    {
        LOG("红黑树全量持久化失败, 建立空的红黑树\n");
    }
    else
    {
        LOG("红黑树全量持久化成功\n");
    }
#endif
}

int kvs_rbtree_destroy(rbtree_t *rbt)
{
    if (!rbt) return -1;
#if (FULL_PERSIST && STORE_FULL)
    // 全量持久化到文件
#if BACK_UP_OLD_FULL_PERSIST_FILE
    if (rb_full_store_with_backup(rbt, RB_FULL_FILE_NAME) == 0)
    {
        printf("红黑树全量文件保存成功\n");
    }
#else
    if (rb_full_store(rbt, RB_FULL_FILE_NAME) == 0)
    {
        printf("红黑树全量文件保存成功\n");
    }
#endif
#endif
    rb_destroy(rbt);
}

int kvs_rbtree_set(rbtree_t *rbt, char *key, char *value)
{
    if (rbt == NULL)
    {
        LOG("rbt:NULL\n");
    }
    if (key == NULL)
    {
        LOG("key: NULL\n");
    }
    if (value == NULL)
    {
        LOG("value: NULL\n");
    }

    if (rbt == NULL || key == NULL || value == NULL)
        return -1;

    int ret = rb_insert(rbt, key, value);
    if (ret == 1)
    {
        // 节点存在
        return 1;
    }
    if (ret == 0)
    {
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "RSET", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: RSET ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: RSET ASYN FAIL\n");
        }
#endif
        return 0;
    }
    if (ret == 1)
    {
        return -1;
    }
}

char *kvs_rbtree_get(rbtree_t *rbt, char *key)
{
    kvs_rbtree_node *node = rb_search(rbt, key);
    if (node == NULL)
    {
        // LOG("NULL\n");
        return NULL;
    }

    else
    {
        return node->value;
    }
}

int kvs_rbtree_del(rbtree_t *rbt, char *key)
{

    if (rbt == NULL || key == NULL)
        return -1;
    int ret = rb_delete(rbt, key);
    if (ret == -1)
    {
        // 节点不存在
        return 1;
    }
    if (ret == 0)
    {
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "RDEL", key, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: RDEL ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: RDEL ASYN FAIL\n");
        }
#endif

        return 0;
    }
}

int kvs_rbtree_all_del(rbtree_t *rbt)
{
    int ret = rb_clear(rbt);
    if (ret == 0)
    {
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "RDELA", NULL, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: RDELA ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: RDELA ASYN FAIL\n");
        }

#endif

        return 0;
    }
    if (ret == -1)
    {
        return -1;
    }

    return 0;
}

int kvs_rbtree_mod(rbtree_t *rbt, char *key, char *value)
{
    if (rbt == NULL || key == NULL || value == NULL)
        return -1;

    int ret = rb_update(rbt, key, value);
    if (ret == 0)
    {
#if AOF_PERSIST

        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "RMOD", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: RMOD ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: RMOD ASYN FAIL\n");
        }

#endif
        return 0;
    }
    if (ret == -1)
    {
        // 节点不存在
        return 1;
    }
}

int kvs_rbtree_exist(rbtree_t *rbt, char *key)
{
    if (rbt == NULL)
        return -1;

    kvs_rbtree_node *node = rb_search(rbt, key);
    if (node == NULL)
    {
        return 1;
    }
    return 0;
}

int kvs_rbtree_count(rbtree_t *rbt)
{
    if (rbt == NULL)
        return -1;

    int count = rb_get_size(rbt);

    return count;
}
